﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Data.Models.MassLine
{
    public class PartyActivityDynamic : EntityBase
    {
        #region 字段属性

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 分类
        /// </summary>
        public string Category { get; set; }

        /// <summary>
        /// 图片ID
        /// </summary>
        public string ImageID { get; set; }

        /// <summary>
        /// 文件ID
        /// </summary>
        public string FileID { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 状态 1草稿 4发布
        /// </summary>
        public int Statue { get; set; }

        /// <summary>
        /// 置顶
        /// </summary>
        public bool? IsTop { get; set; }

        /// <summary>
        /// 头条
        /// </summary>
        public bool? IsFirst { get; set; }

        /// <summary>
        /// 固顶
        /// </summary>
        public bool? SuperTop { get; set; }

        /// <summary>
        /// 是否图片
        /// </summary>
        public bool? IsImage { get; set; }

        /// <summary>
        /// 是否要闻
        /// </summary>
        public bool? IsImportant { get; set; }

        /// <summary>
        /// 是否推送到手机
        /// </summary>
        [StringLength(50)]
        public string IsPushToMobile { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int? OederNoImportant { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人
        /// </summary>
        public string ModifyBy { get; set; }
        #endregion

        #region 获取活动动态
        /// <summary>
        /// 获取活动动态
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="title"></param>
        /// <param name="branchId"></param>
        /// <returns></returns>
        public static DataSet GetActivityDynamic(int pageIndex, int pageSize, string title, string branchId, string category)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            StringBuilder sbsql = new StringBuilder();
            sbsql.Append(@"SELECT SQL_CALC_FOUND_ROWS  data.*,member.UserName,branch.BranchName,IF(data.IsTop=1,'是','否') as IsTopName,
                           IF(data.Statue=4,'是','否') as ShowInHomePageName,IF(data.IsFirst=1,'是','否') as IsFirstName,
                           IF(data.IsImage=1,'是','否') as IsImageName,IF(data.IsImportant=1,'是','否') as IsImportantName from tblPartyActivityDynamic data
                           LEFT JOIN tblpartyorgmember member on Data.CreateBy=member.LoginID
                           LEFT JOIN tblpartyorgbranch branch ON Data.BranchID=branch.BranchID where 1=1 ");
            if (!string.IsNullOrEmpty(title))
            {
                sbsql.Append(" and data.Title like ?Title ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Title",
                        Value = string.Format("%{0}%", title)
                    });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sbsql.Append(" and data.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = branchId
                    });
            }
            if (!string.IsNullOrEmpty(category))
            {
                sbsql.Append(" and data.category=?category ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "category",
                        Value = category
                    });
            }
            sbsql.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString(), parameters.ToArray());
            return ds;
        }

        #endregion

        public static DataTable GetrpHuoDongDongTai(int pageIndex, int pageSize, string filter = "", string orderby = "order by createtime desc")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            StringBuilder sbsql = new StringBuilder();
            sbsql.AppendFormat(@"SELECT SQL_CALC_FOUND_ROWS * FROM tblpartyactivitydynamic where Statue=4 {1} {0}",
                      string.IsNullOrEmpty(orderby) ? "order by createtime desc " : orderby,
                      string.IsNullOrEmpty(filter) ? "" : "and " + filter);

            sbsql.Append(@" LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");


            DataTable dt = MySqlHelper.ExecuteDataSet(sbsql.ToString(), parameters.ToArray()).Tables[0];
            return dt;
        }

        /// <summary>
        /// 头条唯一
        /// </summary>
        /// <param name="ExcludeID">排除ID</param>
        public static void UpdateIsFirst(string ExcludeID)
        {
            string sql = string.Format("UPDATE tblpartyactivitydynamic SET IsFirst=1 where ID!='{0}'; ", ExcludeID);
            MySqlHelper.ExecuteNonQuery(sql);
        }
    }
}

